package com.zxc.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.UserConstants;
import com.heima.model.admin.pojos.ApUserRealname;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AdAuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmUser;
import com.zxc.apis.wemedia.IWemediaClient;
import com.zxc.user.mapper.ApUserMapper;
import com.zxc.user.mapper.ApUserRealnameMapper;
import com.zxc.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional
@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Resource
    private ApUserMapper apUserMapper;

    @Resource
    private IWemediaClient iWemediaClient;

    @Override
    public ResponseResult findUserList(AdAuthDto dto) {

        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<ApUserRealname> wrapper = Wrappers.<ApUserRealname>lambdaQuery();
        if (dto.getStatus() != null) {
            wrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }

        page = page(page, wrapper); // 执行查询

        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal()); // 创建分页响应结果对象
        responseResult.setData(page.getRecords()); // 设置响应结果的数据为查询结果列表


        return responseResult; // 返回响应结果
    }

    @Override
    public ResponseResult updateStatus(AdAuthDto dto, Integer status) {
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaUpdateWrapper<ApUserRealname> wrapper = Wrappers.<ApUserRealname>lambdaUpdate().eq(ApUserRealname::getId, dto.getId()).set(ApUserRealname::getStatus, status);
        if (!StringUtils.isBlank(dto.getMsg())) {
            wrapper.set(ApUserRealname::getReason, dto.getMsg());
        }
        boolean isUpdate = update(wrapper);

        if (!isUpdate) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (status.equals(UserConstants.PASS_AUTH)) {
            ResponseResult responseResult = createWmUser(dto);
            if (responseResult != null) {
                return responseResult;
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private ResponseResult createWmUser(AdAuthDto dto) {
        Integer apUserRealnameId = dto.getId();
        ApUserRealname apUserRealname = getById(apUserRealnameId);
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Integer userId = apUserRealname.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmUser wmUser = iWemediaClient.findUserByName(apUser.getName());

        if (wmUser == null) {
            wmUser = new WmUser();
            wmUser.setApUserId(apUser.getId());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setCreatedTime(new Date());
            wmUser.setName(apUser.getName());
            wmUser.setApAuthorId(apUser.getId());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setPhone(apUser.getPhone());
            iWemediaClient.save(wmUser);

        }
        apUser.setFlag((short) 1);
        apUserMapper.updateById(apUser);
        return null;
    }

    @Override
    public ResponseResult details(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname userRealname = getById(id);
        if (userRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(userRealname);
    }


}
